package com.kratos.designdemo.service.impl;

import com.kratos.designdemo.controller.vo.OrderVo;
import com.kratos.designdemo.service.OrderStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author kratos
 * @date 2022-06-09 14:47:29
 * 订单状态业务适配器
 */
@Component
public class OrderStatusServiceAdapter {

    private List<OrderStatusService> serviceList = new ArrayList<>();

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 注册service
     */
    @PostConstruct
    public void addService() {
        Map<String, OrderStatusService> map = applicationContext.getBeansOfType(OrderStatusService.class);
        for (Map.Entry<String, OrderStatusService> entry : map.entrySet()) {
            serviceList.add(entry.getValue());
        }

    }

    public void updateOrderStatus(OrderVo orderVo) {
        for (OrderStatusService service : serviceList) {
            if (service.support(orderVo)) {
                //return service.update(orderVo);
                service.update(orderVo);
            }

        }

    }
}
